package com.nightpiter.controller.threads;

import android.os.Handler;

import com.nightpiter.controller.MetroController;
import com.nightpiter.controller.algorithms.MetroRouter;
import com.nightpiter.controller.algorithms.Route;
import com.nightpiter.controller.algorithms.exceptions.StationNotFoundDijkstraException;
import com.nightpiter.controller.log.Logger;
import com.nightpiter.model.objects.Station;

/**
 * RouterPreparerThread class.
 * 
 * @author Eldar Abusalimov (eldar.abusalimov@gmail.com)
 */
public class RouterSearcherThread extends Thread {

	/**
	 * station to search routes to
	 */
	private Station toStation;

	/**
	 * MetroRouter instance
	 */
	private MetroRouter router;

	/**
	 * handler from UI thread
	 */
	private Handler handler;

	/**
	 * default constructor
	 */
	public RouterSearcherThread(Station toStation, MetroRouter router,
			Handler handler) {
		super("RouterSearcherThread");
		this.toStation = toStation;
		this.router = router;
		this.handler = handler;
	}

	@Override
	public void run() {
		super.run();
		Route[] routes = null;
		try {
			routes = router.getRoutes(toStation);
		} catch (StationNotFoundDijkstraException e) {
			Logger.logException(e);
		}
		handler.sendMessage(handler.obtainMessage(
				MetroController.MSG_ROUTES_RETRIEVED, routes));
	}

}
